package com.acsior.hibernate.test;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.TreeMap;

/**
 * 
 * @author developer0019
 * 
 */
public class BarcodeGenerator {

	/**
	 * @param args
	 * @throws FileNotFoundException
	 */
	public static void main(String[] args) throws FileNotFoundException {
		fillControlTable();
		
		generateSomePS();
		
		
		TreeMap<String, String> map = new TreeMap<String, String>();
		map.put("35", "OBCENTRDPSLG");
		map.put("36", "OBCENTRDFINI");
		map.put("37", "OBCENTRDLAWA");
		map.put("38", "OBCENTRDHRAB");
		map.put("39", "OBCENTRDSECR");
		map.put("40", "OBCENTRDIFCT");
		map.put("41", "OBCENTRDTRDM");
		map.put("42", "OBCENTRDCONT");
		map.put("43", "OBCENTRDAUDS");
		map.put("44", "OBCENTRUPSTS");
		map.put("45", "OBCENTRULOGS");
		map.put("46", "OBCENTRUSTSR");
		map.put("47", "OBCENTRUECOF");
		map.put("48", "OBCENTRUACCN");
		map.put("49", "OBCENTRUCRLD");
		map.put("50", "OBCENTRULAWR");
		map.put("51", "OBCENTRUPROC");
		map.put("52", "OBCENTRUHUMR");
		map.put("53", "OBCENTRUPRMN");
		map.put("54", "OBCENTRUMBTS");
		map.put("55", "OBCENTRUMSEC");
		map.put("56", "OBCENTRUPTRF");
		map.put("57", "OBCENTRUCIDI");
		map.put("58", "OBCENTRUCIIN");
		map.put("59", "OBCENTRUDTBS");
		map.put("60", "OBCENTRUSAPP");
		map.put("61", "OBCENTRUTCMN");
		map.put("62", "OBCENTRUITMR");
		map.put("63", "OBCENTRUSALE");
		map.put("64", "OBCENTRUMBDV");
		map.put("65", "OBCENTRUIRPM");
		map.put("66", "OBCENTRUFMCN");
		map.put("67", "OBCENTRUCTRR");

		for (String k : map.keySet()) {
			String code = map.get(k);
			int sum = 0;
			for (int i = 0; i < 12; ++i) {
				sum += controlSymTable.get(code.charAt(i));
			}
			String newCode = code + reverseTable.get(sum % 36);
			System.out.println(newCode);
		}

	}
	
	private static void generateSomePS() {
		 Random r = new Random();
			
		 for (String p : prefixes.keySet()) {
		 for (int i = 0; i < prefixes.get(p); ++i) {
		 StringBuilder res = new StringBuilder();
		 res.append(p);
		 int sum = 0;
		 sum += controlSymTable.get(p.charAt(0));
		 sum += controlSymTable.get(p.charAt(1));
		 for (int j = 0; j < 10; ++j) {
		 int ran = r.nextInt(allKeys.size());
		 res.append(allKeys.get(ran));
		 sum += controlSymTable.get(allKeys.get(ran));
		 }
		 // добавяме контролния символ
		 res.append(reverseTable.get(sum % 36));
		 System.out.println(res);
		 }
		 }
	}

	/***/
	public static Map<Character, Integer> controlSymTable = new TreeMap<Character, Integer>();

	/***/
	public static Map<Integer, Character> reverseTable = new TreeMap<Integer, Character>();

	/***/
	public static List<Character> allKeys = new ArrayList<Character>();

	/***/
	public static Map<String, Integer> prefixes = new TreeMap<String, Integer>();

	/**
	 * 
	 */
	public static void fillControlTable() {
		int i = 0;
		char ch = '0';
		for (i = 0; i < 10; i++, ch++) {
			controlSymTable.put(ch, i);
		}
		ch = 'A';
		for (i = 10; i < 36; i++, ch++) {
			controlSymTable.put(ch, i);
		}
		allKeys.addAll(controlSymTable.keySet());

		controlSymTable.put(' ', -1);

		for (Entry<Character, Integer> e : controlSymTable.entrySet()) {
			reverseTable.put(e.getValue(), e.getKey());
		}

		prefixes.put("PS", 100);
		prefixes.put("PP", 0);
		prefixes.put("BS", 0);
		prefixes.put("BA", 0);
		prefixes.put("BO", 0);
		prefixes.put("TR", 0);
		prefixes.put("OB", 0);
		prefixes.put("OP", 0);
		prefixes.put("OR", 0);
		prefixes.put("DS", 0);
		prefixes.put("FN", 0);
		prefixes.put("TA", 0);
		prefixes.put("DV", 0);
		prefixes.put("PD", 0);
		prefixes.put("TS", 0);
		prefixes.put("TT", 0);
		prefixes.put("ZY", 0);
		prefixes.put("MR", 0);
		prefixes.put("PL", 0);
		prefixes.put("BK", 0);
		prefixes.put("EK", 0);
		prefixes.put("HD", 0);
		prefixes.put("HS", 0);
		prefixes.put("HT", 0);
		prefixes.put("TP", 0);
		prefixes.put("##", 0);
		prefixes.put("ZZ", 0);
		prefixes.put("ZW", 0);
		prefixes.put("ZX", 0);
		prefixes.put("OT", 0);
		prefixes.put("ZU", 0);
		prefixes.put("ZV", 0);
		prefixes.put("OA", 0);
	}

}
